package com.zc.cityman.handler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zc.cityman.pojo.Citizen;
import com.zc.cityman.pojo.User;
import com.zc.cityman.service.impl.CitizenServiceImpl;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("citizen")
public class CitizenHandler {
    @Resource
    CitizenServiceImpl citizenService;
    @RequestMapping("citizens")
    @ResponseBody
    public PageInfo<Citizen> dataReport(@RequestParam(value = "pn",defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn,10);
//        System.out.println("分页查询控制器");
        List<Citizen> getall = citizenService.getall();
        PageInfo<Citizen> pageInfo = new PageInfo<>(getall,10); // navigatePages：一次性要显示多少页
        return pageInfo;
    }
    @RequestMapping("delmany")
    public String delmany(@RequestBody List<Integer> ids){
        Integer delmany = citizenService.delmany(ids);
        return delmany>0?"Y":"N";
    }
    @RequestMapping("searct")
    @ResponseBody
    public List<Citizen> search(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "age",required = false)Integer age,
            @RequestParam (value = "starttime",required = false)@DateTimeFormat(pattern = "yyyy-MM-dd") Date starttime,
            @RequestParam(value = "endtime",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd")Date endtime
            ){
        List<Citizen> search = citizenService.search(name, age, starttime, endtime);
        System.out.println(name);
        return search;
    }
    @RequestMapping("datadel")
    public String datadel(@RequestParam("cid") Integer id,@RequestParam(value = "name",required = false) String name,HttpServletRequest httpServletRequest){
        String account =(String) httpServletRequest.getSession().getAttribute("account");
        if (account.equals(name)){
            return "same";
        }else {
            int datadel = citizenService.datadel(id,name);

            return datadel==1?"Y":"N";
        }

    }
    @RequestMapping("insdata")
    @ResponseBody
    public String insert(@RequestBody Citizen citizen,HttpServletRequest httpServletRequest){
        String account = (String)httpServletRequest.getSession().getAttribute("account");
//        if (citizen.getName().equals(account)){
//            return "same";
//        }else{
//            int insert = citizenService.insert(citizen);
//
//            return insert==1?"Y":"N";
//        }
        System.out.println(citizen.getCard());
        List<Citizen> citizens = citizenService.searchByCard(citizen.getCard());

        if(citizens!=null){
            return "same";
        }else{
            int insert = citizenService.insert(citizen);
            return insert==1?"Y":"N";
        }


    }
    @RequestMapping("datasave")
    @ResponseBody
    public String change(@RequestBody Citizen citizen,HttpServletRequest httpServletRequest){
        int change = citizenService.change(citizen);
        return change==1?"Y":"N";
    }
    @RequestMapping ("exit")
    public void exit(HttpServletRequest httpServletRequest){
        httpServletRequest.getSession().invalidate();
    }
    @RequestMapping("accountShow")
    public String accountShow(HttpServletRequest httpServletRequest){
        String account = (String) httpServletRequest.getSession().getAttribute("account");
        return account;
    }
}
